<template>
  <g2-container>
    <el-card class="g2-form">
      <el-form :model="form" :rules="rules" ref="form" label-width="100px" class="demo-ruleForm">
        <el-form-item label="文章标题" prop="title">
          <el-input v-model="form.title"></el-input>
        </el-form-item>
        <el-form-item label="文章副标题" prop="alias">
          <el-input v-model="form.alias"></el-input>
        </el-form-item>
        <el-form-item label="简介" prop="summary">
          <el-input v-model="form.summary"></el-input>
        </el-form-item>
        <el-form-item label="来源" prop="source">
          <el-input v-model="form.source"></el-input>
        </el-form-item>

        <el-row>
          <el-col :span="11">
            <el-form-item label="是否置顶">
              <el-checkbox
                v-model="form.isTop"
                true-label="1"
                false-label="0"
              >{{form.isTop?`置顶`:`不置顶`}}</el-checkbox>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="是否显示">
              <el-checkbox
                v-model="form.isShow"
                true-label="1"
                false-label="0"
              >{{form.isShow?`显示`:`不显示`}}</el-checkbox>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="编译器" required>
              <el-radio-group v-model="form.content_type">
                <!-- <el-radio label="2">UE</el-radio> -->
                <el-radio label="1">markdown</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <!-- <g2-quill :value="form.content"></g2-quill> -->
          <g2-simplemde v-model="form.content"></g2-simplemde>
        </el-row>
        <el-form-item>
          <el-button type="primary" @click="submitForm('form')">立即创建</el-button>
          <el-button @click="resetForm('form')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </g2-container>
</template>

<script>
import g2Quill from "@/components/g2-quill/index.vue";
import g2Simplemde from "@/components/g2-simplemde/index.vue";
import _ from "lodash";

export default {
  name: "g2ArticleUpdate",
  components: {
    g2Quill,
    g2Simplemde
  },
  data() {
    return {
      id: null,
      form: {
        id: null,
        title: "",
        alias: "",
        summary: "",
        source: "",
        content: "",
        content_type: "",
        category_id: "",
        labels: "",
        url: null,
        view_count: 0,
        create_time: "",
        update_time: null,
        is_show: "",
        is_top: ""
      },
      rules: {
        title: [
          { required: true, message: "请输入文章的标题", trigger: "change" }
        ],
        content: [
          { required: true, message: "请填写文章内容", trigger: "change" }
        ]
      }
    };
  },
  computed: {},
  created() {
    this.id = this.$route.params.id;
    if (this.id) {
      this.$store.dispatch("g2/article/getArticleInfo", this.id).then(data => {
   
        this.form.title = data.title;
        this.form.alias = data.alias;
        this.form.summary = data.summary;
        this.form.source = data.source;
        this.form.content = data.content;
        this.form.content_type = data.content_type;
        this.form.category_id = data.category_id;
        this.form.labels = data.labels;
        this.form.url = data.url;
        this.form.view_count = data.view_count;
        this.form.create_time = data.create_time;
        this.form.is_show = data.is_show;
        this.form.update_time = data.update_time;
        this.form.is_top = data.is_top;

        this.oldData = data;
      });
    }
  },

  methods: {
    submitForm(formName) {
      const _this = this;
      this.$refs[formName].validate(valid => {
        if (valid) {
       
          let newObj = {};
          Object.keys(_this.oldData).forEach(item => {
            if (_this.oldData[item] !== _this.form[item]) {
              newObj[item] = _this.form[item];
            }
          });
          if (Object.keys(newObj).length === 0) {
            return;
          }
          newObj.id = _this.oldData.id;

          _this.$store.dispatch(
            "g2/article/updateArticle",
            _.pick(newObj, [
              "id",
              "title",
              "content",
              "summary",
              "source",
              "alias"
            ])
          );
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm() {}
  }
};
</script>

<style scoped lang="scss">
</style>
